<template>
  <lb-layout
    v-loading="$store.getters.loading"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="#a19f9fb0;"
    :paddBottom="5"
  >
    <aps-form
      :config="apsForm.formConfig"
      :model="apsForm.formData"
      :formRef="apsForm.formRef"
      :labelWidth="apsForm.labelWidth"
      ref="form"
    ></aps-form>
    <div class="title">
      <el-tag>操作步骤说明：</el-tag>
      <el-tag>第一步：点击“生成汇总综表数据”生成综合表</el-tag>
      <el-tag>第二步：点击本页面上报按钮，综合表报送上级单位</el-tag>
    </div>
    <lb-table
      :column="tableData.column"
      :data="tableData.data"
      border
      :height="600"
      pagination
      background
      layout="total, sizes, prev, pager, next, jumper"
      :page-sizes="[20, 40, 60, 80]"
      :pager-count="5"
      :current-page.sync="currentPage"
      :total="total"
      :page-size="pageSize"
      @size-change="handleSizeChange"
      @p-current-change="handleCurrentChange"
    >
    </lb-table>
    <component
      :is="modelName"
      :id="id"
      :visible.sync="show"
      @close="close"
    ></component>
  </lb-layout>
</template>

  <script>
import {
  list,
  deleteSummaryList,
  auditSummary,
} from "@/api/work/budgetSummaryList.js";
import auth from "@/plugins/auth";
import operate from "@/views/work/budgetDeclaration/report/components/operate.vue";
export default {
  name: "summary",
  dicts: ["energy_budget_declare_status"],
  components: {
    operate,
  },
  data() {
    return {
      //弹窗
      id: "",
      modelName: "",
      show: false,
      apsForm: {
        formRef: "form",
        labelWidth: "auto",
        formData: {},
        formConfig: {
          formAttr: {
            inline: true,
            size: "small",
          },
          properties: {
            year: {
              label: "年份:",
              typeName: "el-date-picker",
              type: "year",
              size: "small",
              placeholder: "请选择年份",
              "value-format": "yyyy",
            },
          },
          formSoltButton: [
            {
              label: "搜索",
              type: "primary",
              typeName: "el-button",
              size: "small",
              icon: "el-icon-search",
              click: this.handlerSearchDetail,
            },
            {
              label: "重置",
              typeName: "el-button",
              size: "small",
              icon: "el-icon-refresh-left",
              click: this.handleReset,
            },
            {
              label: "生成汇总综表数据",
              type: "primary",
              typeName: "el-button",
              plain: true,
              size: "small",
              icon: "el-icon-plus",
              display: true,
              click: () => this.showModel("operate"),
            },
          ],
        },
      },
      currentPage: 1,
      pageSize: 20,
      total: 0,
      tableData: {
        column: [
          {
            prop: "year",
            label: "预算申报年度",
            minWidth: 120,
            align: "center",
            showOverflowTooltip: true,
          },
          {
            prop: "organizationCounts",
            label: "申报单位数量",
            minWidth: 140,
            align: "center",
            showOverflowTooltip: true,
            // render: (h, scope) => {
            //   let that = this;
            //   let name = scope.row.deptName;
            //   let type = scope.row.summeryTable === 0 ? "2" : "1"; // 0 不是汇总数据 1是汇总数据
            //   let a = (
            //     <router-link
            //       to={
            //         "/work/standbook/reports/collect?deptId=" +
            //         scope.row.deptId +
            //         "&year=" +
            //         scope.row.year +
            //         "&month=" +
            //         scope.row.month +
            //         "&type=" +
            //         type +
            //         "&id=" +
            //         scope.row.id
            //       }
            //       class="link-type"
            //     >
            //       {name}
            //     </router-link>
            //   );
            //   return <div>{a}</div>;
            // },
          },
          {
            prop: "summarizedTime",
            label: "汇总日期",
            minWidth: 120,
            align: "center",
          },
          {
            prop: "status",
            label: "状态",
            minWidth: 120,
            align: "center",
            render: (h, scope) => {
              return (
                <div>
                  <dict-tag
                    options={this.dict.type.energy_budget_declare_status}
                    value={scope.row.status}
                  />
                </div>
              );
            },
          },
          {
            prop: "auditUsername",
            label: "审核人",
            minWidth: 180,
            align: "center",
            showOverflowTooltip: true,
          },
          {
            label: "操作",
            width: 240,
            fixed: "right",
            align: "center",
            render: (h, scope) => {
              let that = this;
              let a, b, c, g;
              let status = scope.row.status; // 台账申报状态
              switch (status) {
                case 0:
                  a = (
                    <el-button
                      v-hasPermi={["energy:budget-summary:audit:1"]}
                      type="text"
                      size="small"
                      icon="el-icon-upload2"
                      onClick={() => {
                        that.reportGo(scope.row.id);
                      }}
                    >
                      上报
                    </el-button>
                  );
                  c = (
                    <el-button
                      v-hasPermi={["energy:budget-summary:delete"]}
                      type="text"
                      size="small"
                      icon="el-icon-delete"
                      onClick={() => {
                        that.handlerDelete(scope.row);
                      }}
                      class="text-danger"
                    >
                      删除
                    </el-button>
                  );
                  break;
                case 6:
                  g = (
                    <el-button
                      v-hasPermi={["energy:budget-summary:audit:4-1"]}
                      type="text"
                      size="small"
                      icon="el-icon-close"
                      onClick={() => {
                        that.handleSubmit(scope.row.id);
                      }}
                      class="text-danger"
                    >
                      确认退回
                    </el-button>
                  );
                  break;
              }
              b = (
                <el-button
                  v-hasPermi={["energy:budget-summary:detail"]}
                  type="text"
                  size="small"
                  icon="el-icon-view"
                  onClick={() => {
                    that.$router.push({
                      name: "budgetSummaryList",
                      query: {
                        id: scope.row.id,
                        type: 2,
                      },
                    });
                  }}
                >
                  查看
                </el-button>
              );

              return (
                <div>
                  {a}
                  {b}
                  {c}
                  {g}
                </div>
              );
            },
          },
        ],
        data: [],
      },
    };
  },
  created() {
    this.judgePermi();
    this.handlerSearchDetail();
  },
  methods: {
    judgePermi() {
      if (auth.hasPermi("energy:budget-summary:summary")) {
        this.apsForm.formConfig.formSoltButton[2].display = false;
      }
    },
    reportGo(id) {
      this.$confirm(`确认要上报吗?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          const data = {
            id,
            eventType: 1,
          };
          auditSummary(data).then((res) => {
            if (res.code === 700) {
              this.$message.warning(res.msg);
              return false;
            }
            this.$message.success("操作成功");
            this.handlerSearchDetail();
          });
        })
        .catch(() => {});
    },
    handleSubmit(id) {
      this.$confirm(`确认要退回吗?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          const data = {
            id,
            eventType: 4,
          };
          auditSummary(data).then((res) => {
            if (res.code === 700) {
              this.$message.warning(res.msg);
              return false;
            }
            this.$message.success("操作成功");
            this.handlerSearchDetail();
          });
        })
        .catch(() => {});
    },
    async initData(data) {
      let that = this;
      let query = {
        ...data,
        orderByColumn: "ebds.declare_time",
        isAsc: "desc",
      };
      query.pageNum = that.currentPage;
      query.pageSize = that.pageSize;
      that.$store.commit("app/updateApiCount", "add");
      await list(query)
        .then((res) => {
          that.$store.commit("app/updateApiCount");
          if (res.code === 700) {
            this.$message.warning(res.msg);
            return false;
          }
          that.tableData.data = res?.rows ?? [];
          that.total = res?.total ?? 0;
        })
        .catch((err) => {
          that.$store.commit("app/updateApiCount");
          // this.$message.error("系统错误,请联系管理员!");
        });
    },
    // 搜索
    handlerSearchDetail() {
      const data = this.$refs?.form?.getFieldsValue();
      this.initData(data);
    },
    // 重置
    handleReset() {
      this.$refs?.form?.resetFields();
      this.handlerSearchDetail();
    },
    handleSizeChange(val) {
      this.currentPage = 1;
      this.pageSize = val;
      this.handlerSearchDetail();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.handlerSearchDetail();
    },
    handlerDelete(row) {
      let that = this;
      let id = row.id;
      const text = "是否确认删除该条数据？";
      this.$modal
        .confirm(text)
        .then(() => {
          that.delectRequest(id);
        })
        .catch(() => {
          that.$message({
            message: "取消删除",
          });
        });
    },
    delectRequest(id) {
      let that = this;
      deleteSummaryList(id)
        .then((res) => {
          if (res.code === 700) {
            this.$message.warning(res.msg);
            return false;
          }
          that.$message.success("已删除!");
          that.handlerSearchDetail();
        })
        .catch((err) => {
          // this.$message.error("系统错误,请联系管理员!");
        });
    },
    // 弹窗
    showModel(name, params) {
      this.modelName = name;
      this.id = params?.id || "";
      setTimeout(() => {
        this.show = true;
      }, 200);
    },
    close(status) {
      this.show = false;
      setTimeout(() => {
        if (status) this.handlerSearchDetail();
      }, 200);
    },
  },
};
</script>

  <style lang="scss" scoped>
::v-deep .title {
  .el-tag {
    margin-right: 10px !important;
    margin-bottom: 20px;
  }
}
</style>
